package com.changfang.gold_man.controller;

import com.changfang.gold_man.common.ImageUtil;
import com.changfang.gold_man.common.ResCode;
import com.changfang.gold_man.common.ResultVO;
import com.changfang.gold_man.entity.Goods;
import com.changfang.gold_man.mapper.GoodsMapper;
import com.changfang.gold_man.service.Imp.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/goods")
@CrossOrigin(value = "*",maxAge = 1800,allowedHeaders = "*")

public class GoodsController {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsServiceImpl goodsServiceImpl;
    //查询所有商品
    @GetMapping("/all")
    public ResultVO getAllGoods() {

            return new ResultVO(ResCode.SUCCESS,goodsServiceImpl.findAll());

    }
    //根据用户id查找商品
    @GetMapping("/id")
    public ResultVO getId(@RequestParam String user_id) {
        try {
            return new ResultVO(ResCode.SUCCESS, goodsServiceImpl.findId(user_id));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }
    //根据商品id查找商品
    @GetMapping("/goodsId")
    public ResultVO getGoodsId(@RequestParam String goods_id) {
        try {
            return new ResultVO(ResCode.SUCCESS, goodsServiceImpl.findGoodsId(Integer.parseInt(goods_id)));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }
    //修改商品信息
    @PostMapping("/update")
    public ResultVO update(@RequestBody Map<String,String> goods) {
        try {
            goodsServiceImpl.update(goods);
            return new ResultVO(ResCode.SUCCESS,"修改商品信息成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "修改商品信息失败");
        }
    }
    //删除商品
    @GetMapping ("/delete")
    public ResultVO deleteGoods(@RequestParam String goods_id) {
        if (goodsServiceImpl.deleteGoods(goods_id).equals("1")){
            return new ResultVO(ResCode.SUCCESS, "删除成功");
        }else {
            return new ResultVO(ResCode.ERROR, "删除失败");
        }

    }
    //商品上架下架
    @GetMapping ("/deal")
    public ResultVO deal(@RequestParam String goods_id,@RequestParam String goods_deal) {
        String flag =  goodsServiceImpl.goodsDeal(goods_id,goods_deal);
        if (flag.equals("1")){
            return new ResultVO(ResCode.SUCCESS, "商品上下架成功");
        }else {
            return new ResultVO(ResCode.ERROR, "商品上下架失败");
        }
    }
    //注册商品
    @PostMapping("/addGoods")
    public ResultVO addGoods(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        if(!file.isEmpty()){
//            System.out.println("上传的图片二进制"+file.getBytes());
            String user_id = request.getParameter("user_id");
            String goods_name = request.getParameter("goods_name");
            String goods_gold = request.getParameter("goods_gold");
            String goods_msg = request.getParameter("goods_msg");
            String goods_deal = "1";
            String goods_img = ImageUtil.fileUpload(file);
            goodsMapper.addGoods(user_id,goods_name,goods_msg,Float.parseFloat(goods_gold),goods_img,goods_deal);
            return new ResultVO(ResCode.SUCCESS, "商品添加成功");
        }
        return new ResultVO(ResCode.ERROR, "商品添加失败");
    }
}
